/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "cmd_input.h"
#include "stdio.h"
#include <string.h>

//参数列表
static char **g_argv = NULL;
//参数列表总数
static int g_argc_total = 0;
//当前需要取出的参数序号
static int g_arg_pos = 0;

int32_t SleGetInputNumberTimeoutMs(const char *info, uint32_t time)
{
    printf("==>%s\r\n", info);
    if (g_arg_pos >= g_argc_total) {
        return 0;
    }
    int num = 0;
    num = atoi(g_argv[g_arg_pos]);
    g_arg_pos++;
    printf("num: %d\r\n", num);
    return num;
}

int32_t SleGetInputNumber(const char *info)
{
    printf("==>%s\r\n", info);
    //当前参数位置不可大于等于参数总数
    if (g_arg_pos >= g_argc_total) {
        return 0;
    }
    int num = 0;
    num = atoi(g_argv[g_arg_pos]);
    //参数顺延一位
    g_arg_pos++;
    printf("num: %d\r\n", num);
    return num;
}

void SleGetInputString(const char *info, char *str, uint32_t size)
{
    printf("==>%s\r\n", info);
    if (g_arg_pos >= g_argc_total) {
        return;
    }
    strncpy(str, g_argv[g_arg_pos], size);
    g_arg_pos++;
    printf("str: %s\r\n",str);
}

void SleSetBufEnv(int argc, char **argv, int pos){
    g_argv = argv;
    g_argc_total = argc;
    g_arg_pos = pos;
}

void SleResetEnv(void){
    g_arg_pos = 0;
    g_argc_total = 0;
    g_argv = NULL;
}
